// <<<<< Tree's XML
//<TREENODE>.subtree = <DIV ...
function treeNodeText(el){
	return Tree.getParentByAttr(el,"id","_tntext") || Tree.getChildByAttr(el,"id","_tntext") ;
}

function treeNodeXML(el){return _treeNodeXML(el);}
function _treeNodeXML(el){return (Tree.getParentByAttr(el,"treenode")||{}).treenode;}

function _parentDIV(el){return Tree.getParentByAttr(el,"className","parent")}
function _chkINPUT(parent){return !parent ? null : parent.all.tags("INPUT")[0]};//.chkBox[0]||parent.all.chkBox}
function _checked(parent,checked){
	var chk = parent.type=="checkbox"?parent:_chkINPUT(parent);
	if( arguments.length > 1 )chk.checked = checked;
	return chk.checked;
}
function _id(node,value){ return __getAttr(node,value) }
function __getAttr(node,value){
	var param = getFuncName(arguments.callee.caller).substr(1);
	if( !defined(value) )return node.getAttribute(param);
	if( value.recordCount )value = value(param).value;
	node.setAttribute(param,value);
	return value;
}
function _nodeAttr(node,attrName,evaluate){
	var v = node.getAttribute(attrName);
	return evaluate ? eval(v) : v;
}
// >>>>> Tree's XML
// Helpers
function defined(v){ return typeof(v)!='undefined' }
function TreeObject(){
	this.getParentByAttr = function(el,attrName,value)
	{
		for( ; el; el = el.parentElement )
			if( el.getAttribute(attrName)!=null && (!defined(value) || el.getAttribute(attrName).match(value)) )return el;
		return null;
	}
	this.getChildByAttr = function(parent,attrName,value)
	{
		if( checkElement(parent,attrName,value) )return parent;
		for( var i = 0; i < parent.all.length; i++ ){
			var el = parent.all[i];
			if( checkElement(el,attrName,value) )return el;
		}
		return null;
	}
	function checkElement(el,attrName,value){
		return el.getAttribute(attrName)!=null && (!defined(value) || el.getAttribute(attrName).match(value))
	}

}
var Tree = new TreeObject();

//document.attachEvent("onkeydown",doEnterKey);
var htcTree = null;
function oncontentreadyEx(tree){
	htcTree = tree;
	htcTree.ondragstart=function(){treeEx_DragStart()};
	htcTree.ondragover=function(){treeEx_OnDrag()};
	htcTree.ondragenter=htcTree.ondragover;
	htcTree.ondrop=function(){treeEx_OnDrop()};
	htcTree.onmousedown=function(){treeEx_preSelect(_eventDragElement())};
	htcTree.onnodebound=function(){treeEx_onDataBound(this)};
	htcTree.systemImagesPath = "/webctrl_client_d/1_0/TreeImages/";
	htcTree.spacerWidth = 0;
	if( treeNodeSrc )treeEx_databind();
}
function collapseAll(collapse){
	var nodes = htcTree.getChildren();
	for( var i=0; i < nodes.length; i++ )
		nodes[i].setAttribute("EXPANDED",!collapse);
}
// ***** DataBind *****
function treeEx_onDataBound(){
	if( htcTree.getChildren().length ){
		htcTree.selectedNodeIndex = '10000';
		setNodes(htcTree,onDataBound.oNodes);
		delete onDataBound.oNodes;
	}
	var ev = createEventObject();
	ev.tree = htcTree;
	nodeBoundEvent.fire(ev);
	//Locals
	function setNodes(node,oNodes){
		if( !oNodes )return;
		if( oNodes[node.getAttribute("ID")] )
			node.setAttribute("EXPANDED",oNodes[node.getAttribute("ID")].expanded);
		var nodes = node.getChildren();
		for( var i=0; i < nodes.length; i++ )
			setNodes(nodes[i],oNodes);
	}
}
function treeEx_databind(dontSaveState){
	onDataBound.oNodes = dontSaveState ? null : getNodes(htcTree);
	htcTree.databind();
	//Locals
	function getNodes(node,oNodes){
		if( !oNodes )oNodes = {};
		oNodes[node.getAttribute("ID")] = {expanded:node.getAttribute("EXPANDED")};
		var nodes = node.getChildren();
		for( var i=0; i < nodes.length; i++ )
			getNodes(nodes[i],oNodes);
		return oNodes;
	}
}
//******* Dragin' ********
var dragDefault;
var DRAG_MOVE = 0;
var DRAG_COPY = 1;
var DRAG_ACTIONS = new Array("move","copy");
function treeEx_preSelect(el){
	if( event.button != 1 )return;
  r = el.document.body.createTextRange();
  r.moveToElementText(el);
  r.select();
  window.event.cancelBubble = true;
  window.event.returnValue = false;
}
function treeEx_DragStart(){
	_dragElement(_eventDragElement());
	window.event.dataTransfer.effectAllowed = "all";
	_dropEffect(dragDefault=DRAG_MOVE);
}
function treeEx_OnDrag(){
  var el = _eventDragElement();
	var nodeXML = treeNodeXML(_dragElement());
	var nodeParent = nodeXML.subtree;
  if( nodeParent.contains(el) || treeNodeXML(el) == nodeXML.previousSibling )return false;
	event.returnValue = false;
	_dropEffect_Auto();
}
function treeEx_OnDrop(){
	var moveCopy = _dragCopy() ? "Copy " : "Move ";
  var elDest = _eventDragElement();
	var nodeDestText = treeNodeText(elDest);
	var nodeDestXML = treeNodeXML(elDest);
  var elSrc = _dragElement();
	var nodeSrcText = treeNodeText(elSrc);
	var nodeSrcXML = treeNodeXML(elSrc);
	if( !confirm(moveCopy+":\n"+nodeSrcText.innerText+"\n\tafter\n"+nodeDestText.innerText+"?") )return;
	var ev = createEventObject();
	ev.nodeSrcXML = nodeSrcXML;
	ev.nodeDestXML = nodeDestXML;
	ev.dragCopy = _dragCopy();
	nodeDropEvent.fire(ev);
}

// Helpers
function transformTreeNodesToSrc(xml,namespace){
	var re = new RegExp((namespace||"MYTREE")+":","g");
	return "<TREENODES>"+xml.replace(re,'')+"</TREENODES>";
}
//function _eventDragElement(){return htcTree.viewLink.elementFromPoint(window.event.clientX,window.event.clientY)}
function _dragElement(el){return arguments.length > 0  ? _dragElement.el = el : _dragElement.el;}
function _dragCopy(){return event.ctrlKey};
function _dropEffect_Auto(){_dropEffect(_dragCopy()?dragDefault^1:dragDefault);}
function _dropEffect(ef){if( defined(ef) )event.dataTransfer.dropEffect = DRAG_ACTIONS[ef];}
function _dragCopy(){return event.ctrlKey};
